package com.naiterui.ehp.bs.doctor.controller.inner;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.bo.recommend.param.RecomBuyNotifyParamBO;
import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.JsonException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.notify.INotifyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 内部服务触发医生通知（push、系统通知（原账户动态）、短信）
 *
 * @author Amber
 * @since 1.0.0
 */
@Controller
@RequestMapping("ad/inner/notify")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.NOTICE_DYNAMICS})
public class AdInnerNotifyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdInnerNotifyController.class);
    @Autowired
    private INotifyService notifyService;

    /**
     * 医生认证状态变更通知</p>
     * 以push、系统通知形式通知用户
     * InnerNotifyController.sendAuthNotify()
     *
     * @param doctorId
     * @param authStatus
     * @param failReason
     *
     * @return
     *
     * @throws BusinessException
     * @Author Amber
     * @Date 2017年8月10日
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping("/auth")
    public ResponseVO<Void> sendAuthNotify(Long doctorId, Integer authStatus, String failReason) throws BusinessException {
        LOGGER.info("医生认证状态变更通知，医生id {},最新状态:{},失败原因{}", doctorId, authStatus, failReason);
        if (null == doctorId || null == authStatus || Doctor.AUTH_STATUS_REJECTED == authStatus && StringUtils.isBlank(failReason)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (Doctor.AUTH_STATUS_REJECTED == authStatus || Doctor.AUTH_STATUS_SUCCESS == authStatus) {
            // 认证通过、不通过之外的状态变更不支持
            notifyService.notifyAuthStatusChange(doctorId, authStatus, failReason);
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    /**
     * 发送购药通知 全购、取消没有有push消息，少购既有push消息也有系统通知
     *
     * @param paramBO
     *
     * @Author heichou
     * @Date 2018年10月18日
     * @since 3.4.0
     * InnerNotifyController.sendBuyNotify()
     */
    @ApiOperation(value = "发送患者购药通知", notes = "发送患者购药通知", httpMethod = "POST")
    @ResponseBody
    @RequestMapping("/recom/buy")
    public ResponseVO<Void> sendBuyNotify(@RequestBody RecomBuyNotifyParamBO paramBO) throws BusinessException {
        try {
            LOGGER.info("患者购药通知，paramBO：{}", JsonMapper.toJson(paramBO));
        } catch (JsonException e) {
            LOGGER.info("患者购药通知，不支持的消息类型");
        }
        if (null == paramBO || null == paramBO.getRecomInfoList()) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        notifyService.notifyRecomBuy(paramBO.getRecomInfoList());
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    /**
     * 发送复诊提醒 应用内通知
     *
     * @param doctorId
     * @param doctorName
     * @param patientName
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("referralNotice/send")
    public ResponseVO<Void> sendReferralNotice(Long doctorId, Long patientId, String doctorName, String patientName) {
        LOGGER.info("发送复诊提醒 应用内通知,doctorId:{},doctorName:{},patientName:{}", doctorId, doctorName, patientName);
        try {
            notifyService.sendReferralNotice(doctorId, patientId, doctorName, patientName);
        } catch (BusinessException e) {
            return PublicService.returnResponseVO(ExceptionCodes.SEND_REFFERALNOTICE_ERROR);
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    /**
     * 发送会话评价通知 应用内通知
     *
     * @param doctorId
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("consult/comment/send")
    public ResponseVO<Void> sendConsultCommentNotice(Long doctorId, Long commentId) throws BusinessException {
        LOGGER.info("发送会话评价通知 应用内通知,doctorId:{}", doctorId);
        notifyService.sendConsultCommentNotice(doctorId, commentId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 发送患者填写随访表单通知 应用内通知
     *
     * @param doctorId
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("followup/send")
    public ResponseVO<Void> sendFollowUpNotice(Long doctorId, Long followUpId, Integer type) throws BusinessException {
        LOGGER.info("发送随访任务开始通知 应用内通知,doctorId:{},followUpId={},type={}", doctorId, followUpId, type);
        notifyService.sendFollowUpNotice(doctorId, followUpId, type);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

}
